/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import interfaces.Constanst;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.Cluster;
import model.Host;
import model.Metrics;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author william
 */
public class Controller implements Constanst {

    public Controller() {
    }

    /**
     * @param name: Nombre del archivo con el que se va a guardar la
     * informacion.
     *
     */
    public String createFile(String name) {
        String result;
        try {
            Socket server = new Socket("192.168.0.60", 8649);
            BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
            result = "" + name + ".xml";
            File xml = new File(result);
            String entrada;
            BufferedWriter bw = new BufferedWriter(new FileWriter(xml));
            while ((entrada = br.readLine()) != null) {
                bw.write(entrada + "\n");
            }
            bw.close();
            server.close();
        } catch (IOException ioe) {
            System.out.print(ioe.toString());
            result = "Ocurrio un problema revisando el puerto o escribiendo el archivo";
        }
        return result;
    }

    public ArrayList<Cluster> clusterAllData() {
        Document doc = null;
        try {
            SAXBuilder domBuilder = new SAXBuilder();
            FileInputStream test = new FileInputStream("" + fileName + ".xml");
            doc = domBuilder.build(test);
        } catch (Exception e) {
            System.out.print(e.toString());
        }
        Element raiz = doc.getRootElement();
        List<Element> clusterList = raiz.getChildren();
        ArrayList<Cluster> clusterArrayList = new ArrayList();
        for (Element clusterHijo : clusterList) {
            List<Element> hostList = clusterHijo.getChildren();
            ArrayList<Host> hostArrayList = new ArrayList();
            for (Element host : hostList) {
                List<Element> metricsList = host.getChildren();
                ArrayList<Metrics> metricsArrayList = new ArrayList();
                long l = (long) Long.parseLong(host.getAttributeValue(reported));
                Host h = new Host(host.getAttributeValue(name), host.getAttributeValue(ip), host.getAttributeValue(owner), new Date(l), (ArrayList<Metrics>) metricsArrayList);
                hostArrayList.add(h);
            }
            Cluster c = new Cluster(clusterHijo.getAttributeValue(name), clusterHijo.getAttributeValue(owner), hostArrayList);
            clusterArrayList.add(c);
        }
        return clusterArrayList;
    }

    public ArrayList ipList(ArrayList<Host> hosts) {
        ArrayList<String> ips = new ArrayList<String>();
        for (Host h : hosts) {
            ips.add(h.getIp());
        }

        return ips;
    }
}
